package com.moran.test.impala.query;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryTime {

    public static void main(String[] args) {

        String sql = "SELECT dc,avg(net_latency) avg_net_latency,COUNT (sid) AS total FROM session.client_session WHERE net_latency > 0 and day = '2017-08-03' and  dc in ('sc','nc') GROUP BY dc";

        Thread myThread1 = new AutoThread(sql,"上屏响应耗时1");
        Thread myThread2 = new AutoThread(sql,"上屏响应耗时2");
        Thread myThread3 = new AutoThread(sql,"上屏响应耗时3");
        Thread myThread4 = new AutoThread(sql,"上屏响应耗时4");
        Thread myThread5 = new AutoThread(sql,"上屏响应耗时5");
        Thread myThread6 = new AutoThread(sql,"上屏响应耗时6");
        Thread myThread7 = new AutoThread(sql,"上屏响应耗时7");
        Thread myThread8 = new AutoThread(sql,"上屏响应耗时8");
        Thread myThread9 = new AutoThread(sql,"上屏响应耗时9");
        Thread myThread10 = new AutoThread(sql,"上屏响应耗时10");
        Thread myThread11 = new AutoThread(sql,"上屏响应耗时11");
        Thread myThread12 = new AutoThread(sql,"上屏响应耗时12");
        Thread myThread13 = new AutoThread(sql,"上屏响应耗时13");
        Thread myThread14 = new AutoThread(sql,"上屏响应耗时14");
        Thread myThread15 = new AutoThread(sql,"上屏响应耗时15");



        myThread1.start();
        myThread2.start();
        myThread3.start();
        myThread4.start();
        myThread5.start();
        myThread6.start();
        myThread7.start();
        myThread8.start();
        myThread9.start();
        myThread10.start();
        myThread11.start();
        myThread12.start();
        myThread13.start();
        myThread14.start();
        myThread15.start();


    }

    private static class AutoThread extends Thread {
        private String sql;
        private String name;


        AutoThread(String sql, String name){
            this.sql = sql;
            this.name = name;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10 ; i++){
                CommonUtil.getResultAndLog(sql,name);
            }
        }
    }

    private static class CommonUtil {

        private final static String url = "jdbc:impala://172.26.5.11:21050/session";

        static {
            try {
                Class.forName("com.cloudera.impala.jdbc41.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        static List<Map<String,Object>> getResult(String sql){
            List<Map<String,Object>> list = new ArrayList<>();
            Connection conn = null;
            Statement statement = null;
            ResultSet resultSet = null;

            try {
                conn= DriverManager.getConnection(url);
                statement = conn.createStatement();
                resultSet = statement.executeQuery(sql);

                ResultSetMetaData metaData = resultSet.getMetaData();
                int metaLength = metaData.getColumnCount();
                while (resultSet.next()){
                    Map<String, Object> map = new HashMap<>();
                    for (int i = 1; i <= metaLength; i++) {
                        String cols_name = metaData.getColumnName(i);
                        Object cols_value = resultSet.getObject(i);
                        if (cols_value == null) {
                            cols_value = "";
                        }
                        map.put(cols_name, cols_value);
                    }
                    list.add(map);
                }
                conn.close();
                statement.close();
                resultSet.close();

            }catch (Exception e){
                e.printStackTrace();
            }

            return list;
        }

        static void getResultAndLog(String sql, String name){
            Long old = System.currentTimeMillis();
            getResult(sql);
            Long now = System.currentTimeMillis();
            System.out.println("SQL耗时:"+name+"-"+(now-old));
            System.out.println("SQL"+sql);
            System.out.println("-----------------------------");
        }

    }
}
